import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

///输入框
///使用时重命名并定制
class AppTextField extends StatefulWidget {
  const AppTextField({
    Key? key,
    this.controller,
    this.prefixIcon,
    this.maxLength,
    this.textInputAction = TextInputAction.done,
    this.keyboardType,
    this.hintText,
    this.onChanged,
    this.onEditingComplete,
    this.onTap,
    this.readOnly = false,
    this.autofocus = false,
    this.obscureText = false,
    this.radius,
    this.maxLines = 1,
    this.height,
    this.initialValue,
    this.inputFormatters,
    this.focusNode,
    this.style,
    this.contentPadding,
    this.iconPadding,
    this.enabled = true,
    this.textAlign = TextAlign.start,
    this.border,
    this.fillColor,
    this.suffixIcon,
  }) : super(key: key);

  final TextEditingController? controller;

  /// 头部控件
  final Widget? prefixIcon;

  /// 尾部控件
  final Widget? suffixIcon;

  /// 最大长度
  final int? maxLength;

  /// 输入模式
  final TextInputAction textInputAction;

  /// 键盘模式
  final TextInputType? keyboardType;

  /// 提示文本
  final String? hintText;

  /// 字体样式
  final TextStyle? style;

  /// 值变化回调
  final Function(String)? onChanged;

  /// 结束编辑事件
  final Function()? onEditingComplete;

  /// 点击事件
  final Function()? onTap;

  /// 是否为只读
  final bool readOnly;

  /// 自动获焦
  final bool autofocus;

  /// 是否为密文
  final bool obscureText;

  /// 圆角
  final double? radius;

  /// 最大行数
  final int maxLines;

  /// 输入框高度
  final double? height;

  /// 默认值
  final String? initialValue;

  /// 输入正则
  final List<TextInputFormatter>? inputFormatters;

  /// 焦点
  final FocusNode? focusNode;

  /// 内边距
  final EdgeInsetsGeometry? contentPadding;

  /// 图标边距
  final EdgeInsetsGeometry? iconPadding;

  /// 是否启用
  final bool enabled;

  /// 文本对齐方向
  final TextAlign textAlign;

  /// 边框
  final InputBorder? border;

  /// 填充背景色
  final Color? fillColor;

  @override
  _AppTextFieldState createState() => _AppTextFieldState();
}

class _AppTextFieldState extends State<AppTextField> {
  /// 隐藏内容
  late bool _hideContent;

  late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _hideContent = false;
    _focusNode = widget.focusNode ?? FocusNode();
  }

  @override
  void dispose() {
    if (widget.focusNode == null) {
      _focusNode.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Widget filed = TextFormField(
      onTap: widget.onTap,
      enabled: widget.enabled,
      textAlign: widget.textAlign,
      focusNode: _focusNode,
      initialValue: widget.initialValue,
      maxLines: widget.maxLines,
      maxLength: widget.maxLength,
      obscureText: _hideContent,
      autofocus: widget.autofocus,
      onChanged: widget.onChanged,
      onEditingComplete: widget.onEditingComplete,
      readOnly: widget.readOnly,
      textInputAction: widget.textInputAction,
      keyboardType: widget.keyboardType,
      controller: widget.controller,
      scrollPadding: EdgeInsets.zero,
      textAlignVertical: TextAlignVertical.center,
      cursorRadius: const Radius.circular(5),
      style: _style,
      decoration: InputDecoration(
        suffixIcon: widget.suffixIcon,
        suffixIconConstraints: const BoxConstraints(
          minHeight: 0,
          minWidth: 0,
        ),
        border: widget.border,
        fillColor: widget.fillColor,
        filled: widget.fillColor != null,
        counterText: '',
        hintText: widget.hintText ?? 'Hint text ...',
        hintStyle: _style,
        contentPadding: widget.contentPadding,
        isDense: true,
      ),
      inputFormatters: widget.inputFormatters,
    );

    if (widget.height != null) {
      filed = SizedBox(
        height: widget.height,
        child: filed,
      );
    }

    return filed;
  }

  TextStyle get _style {
    return widget.style ?? const TextStyle(fontSize: 14);
  }
}
